package com.qf.web.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.pojo.TbGoodsType;
import com.qf.service.GoodTypeService;
import com.qf.service.impl.GoodTypeServiceImpl;
import com.qf.utils.StringConstans;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Create by 周志豪 on 2022/8/18 21:35
 *
 * @authoe 周志豪
 * @veision 1.0
 * //TODO  想好了再去做
 * @since 1.0
 */
public class TypeServlet {

    private GoodTypeService goodTypeService = new GoodTypeServiceImpl();

    private ObjectMapper objectMapper = new ObjectMapper();

    public String findAll(HttpServletRequest request, HttpServletResponse response) {

        List<TbGoodsType> goodsTypeList = (List<TbGoodsType>) request.getServletContext().getAttribute("goodTypes");
        String json = null;
        try {
            json = objectMapper.writeValueAsString(goodsTypeList);
            response.setContentType("application/json;charset=utf-8");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }


    public String add(HttpServletRequest request, HttpServletResponse response) {
        TbGoodsType tbGoodsType = new TbGoodsType();
        int i = goodTypeService.addType(tbGoodsType);
        List<TbGoodsType> goodsTypeList = (List<TbGoodsType>) request.getServletContext().getAttribute("goodTypes");
        goodsTypeList.add(tbGoodsType);
        return StringConstans.REDIRECT + "/admin/showGoodsType.jsp";
    }

}
